package com.bdqn.service;

import com.bdqn.base.mybatis.ServiceImpl;
import com.bdqn.mapper.ShopUserAccountMapper;
import com.bdqn.pojo.ShopUserAccount;
import com.bdqn.pojo.ShopUserAccountCountRateVO;
import org.springframework.stereotype.Service;

/**
 * 用户账户
 *
 * @author lilibo
 * @date 2021/10/01
 */
@Service
public class ShopUserAccountServiceImpl extends ServiceImpl<ShopUserAccountMapper, ShopUserAccount> implements ShopUserAccountService {

    @Override
    public ShopUserAccountCountRateVO getCountRate() {
        ShopUserAccountCountRateVO shopUserAccountCountRateVO = new ShopUserAccountCountRateVO();
        // 获得用户账户表总数
        int userCount = baseMapper.selectCount(null);
        shopUserAccountCountRateVO.setUserCount(userCount);
        // 今天新增用户账户量
        int todayAdd = baseMapper.getTodayAdd();
        // 昨天新增用户账户量
        int yesterdayAdd = baseMapper.getYesterdayAdd();
        // 前七天新增用户账户量
        int weekAdd = baseMapper.getWeekAdd();
        // 前七天新增用户账户平均值
        double weekAddAvg = weekAdd / 7.0;

        // 计算比昨天
        double yesterdayRate = 0;
        if (yesterdayAdd != 0) {
            yesterdayRate = (todayAdd - yesterdayAdd) / yesterdayAdd;
        }
        shopUserAccountCountRateVO.setYesterdayRate(yesterdayRate);
        // 计算比七日
        double weekRate = 0;
        if (weekAddAvg != 0) {
            weekRate = (todayAdd - weekAddAvg) / weekAddAvg;
        }
        shopUserAccountCountRateVO.setWeekRate(weekRate);
        return shopUserAccountCountRateVO;
    }
}
